package com.sf.osms.dep.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dozer.Mapper;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sf.novatar.base.dao.BaseBeanDao;
import com.sf.novatar.base.domain.RequestParams;
import com.sf.osms.dep.dao.IAgentFileMapDao;
import com.sf.osms.dep.domain.AgentFileMap;
import com.sf.osms.dep.vo.AgentVO;

@Repository
public class IAgentFileMapDaoImpl extends BaseBeanDao<Long,AgentFileMap> implements IAgentFileMapDao{
	
	@Autowired
	private Mapper mapper;
	
	@SuppressWarnings("unchecked")
	@Override
	public Map<String,Object> findAgentFileMap(RequestParams createRequestParams) {
		String name = null == createRequestParams.getQueryValue("name") ? "" : createRequestParams.getQueryValue("name");
		String sql = "";		
		  sql += "select new com.sf.osms.dep.vo.AgentVO(tt.id,ta.id,tt.name,ta.fileSuffix,ta.encoding,ta.dataLine,ta.lineSep,"+
		  		"ta.columnSep,ta.mapRule)from AgentFileMap ta,Agent tt"+
                 " where  ta.agentId = tt.id and tt.name like ? order by tt.id desc";
		 Query query=this.getCurrentSession().createQuery(sql);
		 query.setString(0, "%"+name+"%");
		 if(null != query){
			 int limit = Integer.valueOf(createRequestParams.getQueryValue("limit"));
			 int page = Integer.valueOf(createRequestParams.getQueryValue("page"));
			 
			 query.setFirstResult((page-1)*limit);
			 query.setMaxResults(limit);
		 }
		List<AgentVO> listvo  = query.list();
		Map<String,Object> datamap = new HashMap<String,Object>();
		datamap.put("data",listvo );
		datamap.put("count",super.countAll());
		 		
		return datamap;
	}

	@Override
	public AgentFileMap findAgentFileMapByAgentId(Long agentid) {
		String hql = "from AgentFileMap am where am.agentId = ?";
		Query query = this.getCurrentSession().createQuery(hql).setLong(0, agentid);		
		return  query.list().isEmpty() ? null : (AgentFileMap)query.list().get(0);
	}


}
